package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExtHelper;
import com.ruoyi.common.utils.uuid.IdGeneratorSnowflake;
import com.ruoyi.system.domain.TrainingCadetTeacherNotes;
import com.ruoyi.system.domain.TrainingCadetTeacherNotesCount;
import com.ruoyi.system.mapper.TrainingCadetTeacherNotesMapper;
import com.ruoyi.system.service.ITrainingCadetTeacherNotesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 跟师笔记Service业务层处理
 *
 * @author dp
 * @date 2023-11-27
 */
@Service
public class TrainingCadetTeacherNotesServiceImpl implements ITrainingCadetTeacherNotesService {
    @Autowired
    private TrainingCadetTeacherNotesMapper trainingCadetTeacherNotesMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;

    /**
     * 查询跟师笔记
     *
     * @param notesId 跟师笔记主键
     * @return 跟师笔记
     */
    @Override
    public TrainingCadetTeacherNotes selectTrainingCadetTeacherNotesByNotesId(Long notesId) {
        return trainingCadetTeacherNotesMapper.selectTrainingCadetTeacherNotesByNotesId(notesId);
    }

    /**
     * 查询跟师笔记列表
     *
     * @param trainingCadetTeacherNotes 跟师笔记
     * @return 跟师笔记
     */
    @Override
    public List<TrainingCadetTeacherNotes> selectTrainingCadetTeacherNotesList(TrainingCadetTeacherNotes trainingCadetTeacherNotes) {
        return trainingCadetTeacherNotesMapper.selectTrainingCadetTeacherNotesList(trainingCadetTeacherNotes);
    }

    /**
     * 查询跟师笔记合计列表
     * @param submitBegin
     * @param submitEnd
     * @param baseId
     * @param notesStatus
     * @param cadetId
     * @param cadetName
     * @return
     */
    @Override
    public List<TrainingCadetTeacherNotesCount> selectTrainingCadetTeacherNotesCount(
            String submitBegin, String submitEnd, String baseId, String notesStatus, Long cadetId, String cadetName) {
        return trainingCadetTeacherNotesMapper.selectTrainingCadetTeacherNotesCount(
                submitBegin, submitEnd, baseId, notesStatus, cadetId, cadetName);
    }

    /**
     * 根据条件获取跟师笔记列表
     * @param submitBegin
     * @param submitEnd
     * @param baseId
     * @param cadetId
     * @param notesStatus
     * @return
     */
    @Override
    public List<TrainingCadetTeacherNotes> getNoteList(String submitBegin, String submitEnd, String baseId, String cadetId, String notesStatus) {
        return trainingCadetTeacherNotesMapper.getNoteList(submitBegin, submitEnd, baseId, cadetId, notesStatus);
    }

    /**
     * 新增跟师笔记
     *
     * @param trainingCadetTeacherNotes 跟师笔记
     * @return 结果
     */
    @Override
    public int insertTrainingCadetTeacherNotes(TrainingCadetTeacherNotes trainingCadetTeacherNotes) {
        trainingCadetTeacherNotes.setCreateTime(DateUtils.getNowDate());
        if (trainingCadetTeacherNotes.getMentorId() != null && trainingCadetTeacherNotes.getMentorId().isEmpty()) {
            trainingCadetTeacherNotes.setMentorId(null);
        }
        return trainingCadetTeacherNotesMapper.insertTrainingCadetTeacherNotes(trainingCadetTeacherNotes);
    }

    /**
     * 修改跟师笔记
     *
     * @param trainingCadetTeacherNotes 跟师笔记
     * @return 结果
     */
    @Override
    public int updateTrainingCadetTeacherNotes(TrainingCadetTeacherNotes trainingCadetTeacherNotes) {
        if (trainingCadetTeacherNotes.getMentorId() != null && trainingCadetTeacherNotes.getMentorId().isEmpty()) {
            trainingCadetTeacherNotes.setMentorId(null);
        }
        return trainingCadetTeacherNotesMapper.updateTrainingCadetTeacherNotes(trainingCadetTeacherNotes);
    }

    /**
     * 批量删除跟师笔记
     *
     * @param notesIds 需要删除的跟师笔记主键
     * @return 结果
     */
    @Override
    public int deleteTrainingCadetTeacherNotesByNotesIds(Long[] notesIds) {
        return trainingCadetTeacherNotesMapper.deleteTrainingCadetTeacherNotesByNotesIds(notesIds);
    }

    /**
     * 删除跟师笔记信息
     *
     * @param notesId 跟师笔记主键
     * @return 结果
     */
    @Override
    public int deleteTrainingCadetTeacherNotesByNotesId(Long notesId) {
        return trainingCadetTeacherNotesMapper.deleteTrainingCadetTeacherNotesByNotesId(notesId);
    }

    /**
     * 实体数据补全
     *
     * @param trainingCadetTeacherNotes 实体
     * @param userId                    用户id
     * @param crateType                 创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(TrainingCadetTeacherNotes trainingCadetTeacherNotes, Long userId, Integer crateType) {
        //主键
        if (trainingCadetTeacherNotes.getNotesId() == null || trainingCadetTeacherNotes.getNotesId().isEmpty()) {
            trainingCadetTeacherNotes.setNotesId(ExtHelper.toString(idGeneratorSnowflake.nextId()));
        }
        //创建人、创建时间
        if (crateType == 1) {
            if (trainingCadetTeacherNotes.getCreateBy() == null || "".equals(trainingCadetTeacherNotes.getCreateBy())) {
                trainingCadetTeacherNotes.setCreateBy(userId.toString());
            }
            if (trainingCadetTeacherNotes.getCreateTime() == null) {
                trainingCadetTeacherNotes.setCreateTime(DateUtils.getNowDate());
            }
        }
        //修改人
        if (trainingCadetTeacherNotes.getModifyBy() == null) {
            trainingCadetTeacherNotes.setModifyBy(userId);
        }
        //修改时间
        if (trainingCadetTeacherNotes.getModifyTime() == null) {
            trainingCadetTeacherNotes.setModifyTime(DateUtils.getNowDate());
        }
        //培训学员
        if (trainingCadetTeacherNotes.getCadetId() == null || trainingCadetTeacherNotes.getCadetId().isEmpty()) {
            trainingCadetTeacherNotes.setCadetId(userId.toString());
        }
    }
}
